package com.liu.aspect;

import com.fasterxml.jackson.core.type.TypeReference;
import com.liu.annotation.HasPermission;
import com.liu.annotation.HasRole;
import com.liu.constant.Constants;
import com.liu.core.RedisTemplate;
import com.liu.exception.PermissionNeedHasException;
import com.liu.exception.RoleNeedHasException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.logging.Handler;

/**
 * @ClassName PermissionAspect
 * @Author liuxin
 * @Date Created in 2023-01-30 15:30
 * @Version 1.0
 */
@Component
//声明为一个切面
@Aspect

public class PermissionAspect {
    /**
     * 执行接口所需角色的切面
     */

    @Resource
    RedisTemplate redisTemplate;
    @Before("@annotation(hasRole)")
    public void roleBefore(JoinPoint joinPoint, HasRole hasRole){
        //获得当前方法所需要的角色
        String[] needRoles = hasRole.value();
        //获得拥有的角色
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        //获取首部信息的token
        String token = request.getHeader(Constants.TOKEN_AUTHORIZATION);
        //获取角色
        List<String> hasRoles = redisTemplate.getObject(Constants.ROLE_PREFIX + token, new TypeReference<List<String>>() {});

        //只要所需的角色有一个在你拥有的角色中就放行
        boolean flag = false;
        for(String needRole : needRoles){
            if(hasRoles.contains(needRole)){
                flag = true;
                break;
            }
        }
        if(!flag ) {
            throw new RoleNeedHasException("您没有该接口锁需要的角色");
        }
    }

    @Before("@annotation(hasPermission)")
    public void roleBefore(JoinPoint joinPoint, HasPermission hasPermission){
        //获得当前方法所需要的权限
        String[] needPermissions = hasPermission.value();
        //获得拥有的角色
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        //获取首部信息的token
        String token = request.getHeader(Constants.TOKEN_AUTHORIZATION);
        //获取角色
        List<String> hasRoles = redisTemplate.getObject(Constants.PERM_PREFIX + token, new TypeReference<List<String>>() {});
        System.out.println("hasRoles: " + hasRoles);
        System.out.println("needPermissions: " + Arrays.toString(needPermissions));
        //只要所需的角色有一个在你拥有的角色中就放行
        boolean flag = false;
        for(String needPermission : needPermissions){
            if(hasRoles.contains(needPermission)){
                System.out.println("进入了true");
                flag = true;
                break;
            }
        }
        if(!flag ) {
            throw new PermissionNeedHasException("您没有该接口锁需要的角色");
        }
    }
}
